<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'佣金列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array(0,1,4,5,6,7));
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('store_commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$order=pdo_get('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'status');
			if($order['status']==3 && !$item['status']){
				$this->update('store_commission',array('status'=>1),array('id'=>$item['id'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array(
			'tip'=>'',
			'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'storetitle','title'=>'门店','type'=>'string'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'money','title'=>'门店分佣','type'=>'string'),
			array('name'=>'jifen','title'=>'门店币奖励额','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'type','title'=>'佣金类型','type'=>'radio','options'=>'commission_types'),
			array('name'=>'status','title'=>'佣金状态','type'=>'radio','options'=>'commission_status'),
			array('name'=>'complete_time','title'=>'订单完成时间','type'=>'time'),
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'change','title'=>'修改佣金'),
		),'batch'=>array(
			array('type'=>'modal','url'=>'/store.commission/jiesuan/','title'=>'一键结算（可结算的）'),
			array('type'=>'post','url'=>'jy_weishop/store.commission/upfee/','title'=>'刷新异常订单'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'佣金状态','options'=>'commission_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['commission_status']=$this->commission_status;
	$return['commission_types']=$this->commission_types;
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('store_commission',array('money'=>$post['money']),array('id'=>$id));
		exi('修改佣金成功！','','list');
	}
	$item=$this->get('store_commission',array('id'=>intval($_RQ['id'])),array('id','money'));
	if(!$item){
		exi('指定佣金不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改佣金',
		'edit'=>array(
			array('type'=>'string','name'=>'money','title'=>'设置佣金','help'=>'佣金异常时操作'),
		),
	);
	exi($return);
}
if($_RQ['op']=='upfee'){
	$orders=$this->getall('order',array('createtime >'=>1632816022,'uniacid'=>$_SESSION['uniacid'],'storeid >'=>0));
	foreach($orders as $order){
		$isup=$this->getcolumn('store_commission',array('ordersn'=>$order['ordersn']),'id');
		if($isup){
			continue;
		}
		$order['goods']=$this->getall('order_goods',array('orderid'=>$order['id']));
		$this->model('store')->up_commission($order);
	}
	exi('一键恢复成功！');
}
if($_RQ['op']=='jiesuan'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$this->model('store')->create_balance('',$post['sendto']);
		exi('一键结算成功！');
	}
	$return['item']=array('sendto'=>1);
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'一键结算',
		'edit'=>array(
			array('name'=>'sendto','title'=>'结算方式','type'=>'radio','options'=>'sendto'),
		),
	);
	$return['sendto']=array('0'=>'平均分给店员','1'=>'打款给店长');
	exi($return);
	
}